/*
 * Logger.cpp
 *
 *  Created on: 7 июн. 2022 г.
 *      Author: nikita
 */

#include "Logger.h"

namespace STC_calc
{

std::string Logger::file_name = "~default";
std::string Logger::buffer = "";
bool Logger::is_defined = false;
std::function<void(const std::string&)> Logger::outfunc = &write_to_terminal;

void Logger::start_message()
{
	put("Hello world! It's the debugging built. \n");
	put("There could be unnecessary commentaries and tests \n");
	put("If You need the release built, remove the DEBUG macros in the main.cpp. \n");
	put(std::string(69, 43) + "\n\n");
}

void Logger::write_all()
{
	outfunc(buffer);
}

void Logger::write_to_terminal(const std::string& str)
{
	std::cout << str;
}
void Logger::write_to_file(const std::string& str)
{
	FileManager::save_log(str, file_name);
}

Logger::Logger()
{
}
Logger::~Logger()
{
}

void Logger::help()
{
	put("help");
	put("help");
	put("help");
	put("help");
	put("help");
}

void Logger::set_global_file(const std::string& name)
{
	if(is_defined) throw(RuntimeErrorClass("Global logger can be initialized only once"));
	is_defined = true;
	if (name == "~default%terminal")
	{
		outfunc = &write_to_terminal;
		return;
	}
	if (name == "~default%file")
	{
		outfunc = &write_to_file;
		file_name = "~default";
		return;
	}
	file_name = name;
	outfunc = &write_to_file;
}

void Logger::put(const std::string& str)
{
	if(!is_defined)
	{
		buffer += "Warning: Global Logger is undefined. Standart output";
		set_global_file();
	}
	buffer += str + "\n";
}

} /* namespace STC_calc */
